using System;
using System.Diagnostics;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Exusiai.Editor.Models;
using Avalonia.Exusiai.Editor.ViewModels;
using Avalonia.Exusiai.Editor.Views;
using Avalonia.Markup.Xaml;
using Avalonia.Media;

namespace Avalonia.Exusiai.Editor;

public class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new SplashWindow(() =>
            {
                var mainWindow = new MainWindowView()
                {
                    DataContext = new MainWindowViewModel()
                };
                mainWindow.Show();
                mainWindow.WindowState=WindowState.Maximized;
                mainWindow.Focus();
                desktop.MainWindow = mainWindow;
            });
            //修改默认字体
            desktop.MainWindow.FontFamily = new FontFamily("Source Han Sans SC");
        }

        base.OnFrameworkInitializationCompleted();

        // Enable Avalonia DevTools
#if DEBUG
        (Current ?? throw new InvalidOperationException()).AttachDevTools();
#endif
    }

    public static async void CopyText(string data)
    {
        if(Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            if (desktop.MainWindow.Clipboard is { } clipbord)
                await clipbord.SetTextAsync(data);
        }
    }

    public static void ShowInExplorer(string path)
    {
        Process.Start("explorer.exe", $"/select, \"{path}\"");
    }

    public static void UseDefaultEditor(string path)
    {
        Process.Start(new ProcessStartInfo
        {
            FileName = path,
            UseShellExecute = true
        });
    }
}